package com.bocai.eelly.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bocai.eelly.R;
import com.bocai.eelly.utils.BitmapUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ChoosePhotosAda extends BaseAdapter {

    private Context context;
    ArrayList<String> photos =new ArrayList<>();
    private int what;
    public  ChoosePhotosAda(Context context,ArrayList<String> photos,int what) {
        this.context = context;
        this.photos=photos;
        this.what=what;
    }

    @Override
    public int getCount() {
//        int count;
//        if(what==0){
//            if(photos.size()==0){
//                count=1;
//            }else{
//                count=photos.size();
//            }
//        }else{
//            if(photos.size()<5){
//                count=photos.size()+1;
//            }else{
//                count=photos.size();
//            }
//        }
        return photos.size()+1;
    }

    @Override
    public Object getItem(int position) {
        return photos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.choose_photo_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else
        {
            viewHolder= (ViewHolder) convertView.getTag();
        }

//        if(what==0){
//            if(photos.size()==0){
//                if(position==photos.size()){
////            viewHolder.ivPhotoChoose.setOnClickListener(new MyOnclickListener(position));
//                    viewHolder.ivPhotoChoose.setImageResource(R.mipmap.ic_choose_photo);
//                    viewHolder.ivPhotoChooseDismiss.setVisibility(View.GONE);
//                }else {
//                    viewHolder.ivPhotoChooseDismiss.setVisibility(View.VISIBLE);
//                    viewHolder.ivPhotoChoose.setImageBitmap(BitmapUtils.compressBitmap(photos.get(position)));
//                    viewHolder.ivPhotoChooseDismiss.setOnClickListener(new MyOnclickListener(position));
//                }
//            }else{
//                viewHolder.ivPhotoChooseDismiss.setVisibility(View.VISIBLE);
//                viewHolder.ivPhotoChoose.setImageBitmap(BitmapUtils.compressBitmap(photos.get(position)));
//                viewHolder.ivPhotoChooseDismiss.setOnClickListener(new MyOnclickListener(position));
//            }
//
//        }else{
//            if(photos.size()<5){
//                if(position==photos.size()){
////            viewHolder.ivPhotoChoose.setOnClickListener(new MyOnclickListener(position));
//                    viewHolder.ivPhotoChoose.setImageResource(R.mipmap.ic_choose_photo);
//                    viewHolder.ivPhotoChooseDismiss.setVisibility(View.GONE);
//                }else {
//                    viewHolder.ivPhotoChooseDismiss.setVisibility(View.VISIBLE);
//                    viewHolder.ivPhotoChoose.setImageBitmap(BitmapUtils.compressBitmap(photos.get(position)));
//                    viewHolder.ivPhotoChooseDismiss.setOnClickListener(new MyOnclickListener(position));
//                }
//            }else{
//                viewHolder.ivPhotoChooseDismiss.setVisibility(View.VISIBLE);
//                viewHolder.ivPhotoChoose.setImageBitmap(BitmapUtils.compressBitmap(photos.get(position)));
//                viewHolder.ivPhotoChooseDismiss.setOnClickListener(new MyOnclickListener(position));
//            }
//
//        }

        if(position==photos.size()){
//            viewHolder.ivPhotoChoose.setOnClickListener(new MyOnclickListener(position));
            viewHolder.ivPhotoChoose.setImageResource(R.mipmap.ic_choose_photo);
            viewHolder.ivPhotoChooseDismiss.setVisibility(View.GONE);
        }else {
            viewHolder.ivPhotoChooseDismiss.setVisibility(View.VISIBLE);
            viewHolder.ivPhotoChoose.setImageBitmap(BitmapUtils.compressBitmap(photos.get(position)));
            viewHolder.ivPhotoChooseDismiss.setOnClickListener(new MyOnclickListener(position));
        }
        return convertView;
    }


    public class MyOnclickListener implements View.OnClickListener{
        private int position;
        public  MyOnclickListener(int position){
            this.position=position;
        }
        @Override
        public void onClick(View v) {
            photos.remove(position);
            notifyDataSetChanged();
        }
    }
    class ViewHolder {
        @Bind(R.id.iv_photo_choose)
        ImageView ivPhotoChoose;
        @Bind(R.id.iv_photo_choose_dismiss)
        ImageView ivPhotoChooseDismiss;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
